package com.lab303.powerproject.controller;


import com.lab303.powerproject.entity.GraphInfo;
import com.lab303.powerproject.service.DeviceService;
import com.lab303.powerproject.service.GraphInfoService;
import com.lab303.powerproject.service.RelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("graphInfo")
@Controller
public class GraphInfoController {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private RelationshipService relationshipService;

    /**
     * 查看所有图谱列表页面,根据管理者管理楼栋进行筛选
     * @param model
     * @return
     */
    @GetMapping("/viewGraphInfo")
    public String viewGraphInfo(Model model){
        List<GraphInfo> allGraphInfo = graphInfoService.getAllGraphInfo();
        //List<GraphInfo> allGraphInfo = graphInfoService.findGraphInfosByRemark(remark);
        model.addAttribute("graphInfoList",allGraphInfo);
        return "knowChart";
    }

    /**
     * 获取指定id的图谱信息页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/get/{id}")
    public String getGraphInfo(@PathVariable("id")Integer id, Model model){
        GraphInfo graphInfoById = graphInfoService.findGraphInfoById(id);
        model.addAttribute("graphInfo",graphInfoById);
        return "knowChartEdit";
    }

    /**
     * 保存一个新增图谱信息
     * @param graphInfo
     * @return
     */
    @PostMapping("/addGraphInfo")
    @ResponseBody
    public Map<String, String> addGraphInfo( GraphInfo graphInfo){
        Map<String,String> res = new HashMap();
        String message = "success";
        GraphInfo info = graphInfoService.addOneGraphInfo(graphInfo);
        if (info==null){
            message = "fail";
        }
        res.put("message",message);
        return res;
    }

    /**
     * 更新图谱基本信息
     * @param graphInfo
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, String> updateGraphInfo( GraphInfo graphInfo){
        Map<String,String> res = new HashMap();
        String message = "success";
        GraphInfo info = graphInfoService.saveAndFlush(graphInfo);
        if (info==null){
            message = "fail";
        }
        res.put("message",message);
        return res;
    }

    /**
     * 删除一张图谱，包括删除图谱中的设备，图谱的边，还有图谱的基本信息
     * @param graphId
     * @return
     */
    @GetMapping("/delete/{graphId}")
    @ResponseBody
    public Map deleteGraphInfo(@PathVariable("graphId")Integer graphId){
        graphInfoService.deleteAllGraphInfoByGraphId(graphId);
        Map res  =new HashMap();
        res.put("message","delete_success");
        return res;
    }

    /**
     * 图谱编辑提交的处理接口，把拖拽好的图谱信息更新，包括更新边，更新设备的坐标，以及更新设备图谱标志
     * @param graphId
     * @param xmlString
     * @return
     */
    @PostMapping("/updateGraph")
    @ResponseBody
    public Map updateGraph(Integer graphId,@RequestParam("xmlString")String xmlString){
        String s = graphInfoService.updateGraphByXmlString(graphId, xmlString);
        Map map = new HashMap();
        map.put("message",s);
        return map;
    }

    /**
     * 跳转图谱拖拽界面
     * @param graphId
     * @param model
     * @return
     */
    @GetMapping("/viewChartDrawPage/{graphId}")
    public String viewChartDrawPage(@PathVariable("graphId")Integer graphId,Model model){
        model.addAttribute("graphId",graphId);
        return "chartDraw";
    }
}
